<?php

namespace Wang\TonglianPay\encrypt;

class Encrypt
{

    public function sign(String $data, String $key, String $pass)
    { # 签名
        openssl_pkcs12_read(file_get_contents($key), $priKey, $pass);
        if (!$priKey) return false;
        return openssl_sign($data, $sign, $priKey['pkey']) ? base64_encode($sign) : false;
    }

    /**
     * web端RSA加密
     * @param String $data
     * @param $pubKey
     * @return String
     */
    public function webRsaEncrypt(String $data,$pubKey): String
    {
        $pubKey = openssl_get_publickey(file_get_contents($pubKey));
        return openssl_public_encrypt($data, $res, $pubKey, OPENSSL_PKCS1_PADDING) ? base64_encode($res) : false;
    }

    /**
     * RSA加密
     * @param String $data
     * @param String $key
     * @return String
     */
    public function rsaEncrypt(String $data, String $key): String
    {
        $pubKey = file_get_contents($key);
        return openssl_public_encrypt($data, $res, $pubKey) ? base64_encode($res) : false;
    }

    /**
     * RSA解密
     * @param String $data
     * @param String $key
     * @param String $pass
     * @return String
     */
    public function rsaDecrypt(String $data, String $key, String $pass)
    {
        openssl_pkcs12_read(file_get_contents($key), $priKey, $pass);
        if (!$priKey) return false;
        openssl_private_decrypt($data, $res, $priKey['pkey']);
        return $res;
    }

    /**
     * AES加密
     * @param String $data
     * @param String $key
     * @return String
     */
    public function aesEncrypt(String $data, String $key): String
    {
        # 补空格
        $data .= str_repeat("\x20", 16 - strlen($data) % 16);
        # 补空字符 == C中\0结尾
        # $data .= str_repeat("\x0",16 - strlen($data) % 16);
        return base64_encode(openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_NO_PADDING, '1234567812345678'));
    }

    /**
     * AES解密
     * @param String $data
     * @param String $key
     * @return String
     */
    public function aesDecrypt(String $data, String $key): String
    {
        return openssl_decrypt($data, 'AES-128-CBC', $key, OPENSSL_NO_PADDING, '1234567812345678');
    }

    /**
     * des解密
     * @param $data
     * @param $key
     * @return false|string
     */
    public function desDecrypt($data, $key)
    {
        return openssl_decrypt($data, 'DES-EDE3', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING);
    }

    /**
     * des加密
     * @param string $desStr
     * @param String $key
     * @return string
     */
    public function desEncrypt(string $desStr, String $key): string
    {
        if (strlen($desStr) % 8) $desStr = str_pad($desStr, strlen($desStr) + 8 - strlen($desStr) % 8, "\0");
        return base64_encode(openssl_encrypt($desStr, 'DES-EDE3', $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING));
    }

    /**
     * 效验數據
     * @param String $data
     * @param String $sign
     * @param String $key
     * @return bool
     */
    public function verify(String $data, String $sign, String $key): bool
    {
        $publicKey = openssl_pkey_get_public(file_get_contents($key));
        return openssl_verify($data, base64_decode($sign), $publicKey, OPENSSL_ALGO_SHA1);
    }
}